package com.sky.service.impl;

import com.sky.entity.Orders;
import com.sky.mapper.ReportMapper;
import com.sky.service.WorkspaceService;
import com.sky.vo.BusinessDataVO;
import com.sky.vo.DishOverViewVO;
import com.sky.vo.OrderOverViewVO;
import com.sky.vo.SetmealOverViewVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;


@Service
public class WorkspaceServiceImpl implements WorkspaceService {
    @Autowired
    private ReportMapper reportMapper;

    @Override
    public BusinessDataVO getBusinessData(LocalDate localDate) {

        String dateStr = localDate.toString() + "%";
        //新增用户数
        Integer newUsers = reportMapper.sumNewUserLikeTime(dateStr);
        newUsers = newUsers == null ? 0 : newUsers;
        //有效订单
        Integer validOrderCount = reportMapper.sumOrdersLikeTime(5, dateStr);
        validOrderCount = validOrderCount == null ? 0 : validOrderCount;
        //营业额
        Double turnover = reportMapper.sumAmountLikeTime(dateStr);
        turnover = turnover == null ? 0 : turnover;
        //订单完成率
        Integer orderAll = reportMapper.sumOrdersLikeTime(null, dateStr);
        Double orderCompletionRate = (validOrderCount * 1.0) / orderAll;
        orderCompletionRate = orderCompletionRate.isNaN() ? 0 : Double.valueOf(String.format("%.2f", orderCompletionRate));
        //平均客单价
        Double unitPrice = (turnover * 1.0) / validOrderCount;
        unitPrice = unitPrice.isNaN() ? 0 : Double.valueOf(String.format("%.2f", unitPrice));

        return BusinessDataVO.builder()
                .newUsers(newUsers)
                .validOrderCount(validOrderCount)
                .turnover(turnover)
                .orderCompletionRate(orderCompletionRate)
                .unitPrice(unitPrice)
                .build();
    }

    @Override
    public DishOverViewVO getOverviewDishes() {

        // 已启售数量
        Integer sold = reportMapper.sumDishs(1);
        // 已停售数量
        Integer discontinued = reportMapper.sumDishs(0);

        return DishOverViewVO.builder()
                .discontinued(discontinued)// 已停售数量
                .sold(sold)// 已启售数量
                .build();
    }

    @Override
    public SetmealOverViewVO getOverviewSetmeals() {
        // 已启售数量
        Integer sold = reportMapper.sumSetmeals(1);
        // 已停售数量
        Integer discontinued = reportMapper.sumSetmeals(0);

        return SetmealOverViewVO.builder()
                .discontinued(discontinued)// 已停售数量
                .sold(sold)// 已启售数量
                .build();

    }
    @Override
    public OrderOverViewVO getOverviewOrders() {

        //待接单数量
        Integer waitingOrders = reportMapper.getOrderByStatus(Orders.TO_BE_CONFIRMED);

        //待派送数量
        Integer deliveredOrders = reportMapper.getOrderByStatus(Orders.CONFIRMED);

        //已完成数量
        Integer completedOrders = reportMapper.getOrderByStatus(Orders.COMPLETED);

        //已取消数量
        Integer cancelledOrders = reportMapper.getOrderByStatus(Orders.CANCELLED);

        //全部订单
        Integer allOrders = reportMapper.getOrderByStatus(null);

        return OrderOverViewVO.builder()
                .allOrders(allOrders)
                .cancelledOrders(cancelledOrders)
                .completedOrders(completedOrders)
                .deliveredOrders(deliveredOrders)
                .waitingOrders(waitingOrders)
                .build();
    }


}
